package com.example.hezhe_backend.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.example.hezhe_common.pojo.User;
import com.example.hezhe_common.utils.AliyunObjectUtils;
import com.example.hezhe_interface.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private UserService userService;


    //用户注册
    @RequestMapping("/register")
    public String register(String username, String password, String telephone, String code) {
        String codeInRedis = jedisPool.getResource().get(telephone);
        if (code == null || codeInRedis == null || !code.equals(codeInRedis)) {
            return "您输入的验证码有误，请检查后重新输入";
        } else {
            if (userService.findByUsername(username) != null) {
                return "该用户名已存在！";
            }
            User user = new User();
            String encodePassword = new BCryptPasswordEncoder().encode(password);
            Date date = new Date();
            user.setCreateTime(date);
            user.setUsername(username);
            user.setPassword(encodePassword);
            user.setTelephone(telephone);
            user.setIdentity("USER");
            user.setHead("DefaultHead.png");
            userService.add(user);
            User flag = userService.findByUsername(username);
            if (flag == null) {
                return "注册失败";
            } else {
                return "注册成功";
            }
        }
    }

    //普通修改密码
    @RequestMapping("/changePsd")
    public String changePsd(String username, String beforePsd, String newPsd) {
        User beforeUser = userService.findByUsername(username);
        if (beforeUser == null) {
            return "该用户不存在！";
        }
        if (!new BCryptPasswordEncoder().matches(beforePsd, beforeUser.getPassword())) {
            return "原密码输入错误！";
        }
        userService.changePsd(username, new BCryptPasswordEncoder().encode(newPsd));
        return "修改成功！";
    }

    //忘记密码
    @RequestMapping("/forgetPsd")
    public String forgetPsd(String telephone, String code, String newPsd) {
        if (userService.findByTelephone(telephone)==null){
            return "该用户尚未注册";
        }
        if (jedisPool.getResource().get(telephone + "FRT") == null || !code.equals(jedisPool.getResource().get(telephone + "FRT"))) {
            return "验证码有误";
        }
        userService.forgetPsd(telephone,new BCryptPasswordEncoder().encode(newPsd));
        return "修改成功";
    }


    //更换头像
    @RequestMapping("/changeHead")
    public String changeHead(@RequestParam("ImgFile") MultipartFile multipartFile, @RequestParam("username") String username) {
        String originalFilename = multipartFile.getOriginalFilename();//获取原始文件名
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);//.png或.jpg

        String fileName = UUID.randomUUID().toString() + extention;//adadadads.jpg
        try {
            AliyunObjectUtils.uploadToAliyun(multipartFile.getBytes(), fileName);
            if (userService.findByUsername(username) != null) {
                userService.changeHead(fileName, username);
                return "更新头像成功！";
            } else {
                return "请检查用户名是否输入正确！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "更新头像失败！";
    }
}
